#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import jsonpath
import requests

def initlist(json_str):
    # 1初始化init
    items = []
    # 将json格式'str字符串'转化为python对象
    py_json = json.loads(json_str)
    # print(py_json)
    comments_list = py_json['comments']
    for comment in comments_list:
        # 使用jsonpath方式查找
        user = jsonpath.jsonpath(comment, '$..user')[0]
        # 用户头像
        user_face = 'http;' + user['avatar']
        # print(user_face)
        #用户名
        user_name=user['nick']
        # print(user_name)
        #评论内容
        user_text=comment['content']
        #评论日期
        user_date=comment['date']
        #手机信息
        info=jsonpath.jsonpath(comment, '$..sku')[0]
        #评论视频截图,有的没有视频video就是NULL值，所以加一个判断
        if 'VideoUrlideo' in comment:
            # 评论截图和视频
            videoimg = comment['video']['coverUrl']
            videoUrl = comment['video']['coverUrl']
        else:
            videoimg = None
            videoUrl = None

        #好评图片（comment['photos']是一个列表）
        imglist = []
        goodimglist = comment['photos']
        if goodimglist:
            for i in goodimglist:
                imglist.append('http;' +i['url'])

        item = {
            '评论人': user_name,
            '评论头像': user_face,
            '评论时间': user_date,
            '评论内容': user_text,
            '购买商品类型': info,
            '评论视频截图': videoimg,
            '评论视频': videoUrl,
            '好评图片': imglist,
        }
        # 3 append是加入到list
        items.append(item)
    return items


def tbmain(id,page):
    url = f'https://rate.taobao.com/feedRateList.htm?auctionNumId={id}&userNumId=903511695&currentPageNum={page}&pageSize=20'
    headers = {
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Referer': 'https://item.taobao.com/item.htm?spm=a230r.1.14.31.1a9df9eaZ2TtQ2&id=593014213362&ns=1&abbucket=4',

        'Cookie': 'miid=1034753504511266755;cna=dx1nFLT5mQcCAXWaR2QxwmBW;'

                  'thw=cn;t=325ff4e457909046382b22a33c83eadb;tracknick=acc%5Cu80E1%5Cu4F20;'

                  'lgc=acc%5Cu80E1%5Cu4F20;tg=0;v=0;'

                  'cookie2=128cea00303f12de470490c850cc00b1;_tb_token_=fdb4b335af83e;'

                  'unb=2701405241;sg=%E4%BC%A01a;_l_g_=Ug%3D%3D;skt=051d1e62c9ab8dc5;'

                  'cookie1=BxMH3NLGjomw6rJWdejLCTcGCFEkYwy%2BoQnBejFVBZI%3D;csg=93fdcb74;'

                  'uc3=vt3=F8dBy3jYcBxWmWeUeDY%3D;id2=UU8IPT1oZbApWA%3D%3D;nk2=AnIsGxSURQ%3D%3D;lg2=Vq8l%2BKCLz3%2F65A%3D%3D;'

                  'existShop=MTU1OTY1MDAxNw%3D%3D;_cc_=U%2BGCWk%2F7og%3D%3D;dnk=acc%5Cu80E1%5Cu4F20;_nk_=acc%5Cu80E1%5Cu4F20;'

                  'cookie17=UU8IPT1oZbApWA%3D%3D;mt=ci=6_1&np=;'

                  'enc=UFEUuOrXKTfwHDdKWP%2F9JacRMgPfTTQxKHxhkY8FExWK0%2FOKPqyryg048bj3QRjP%2Blwvl2%2BmU0N0pIBcrlD2PA%3D%3D;'
                  'hng=CN%7Czh-CN%7CCNY%7C156;'
                  'uc1=cookie15=VT5L2FSpMGV7TQ%3D%3D&cookie14=UoTZ7Yy7WSTGKg%3D%3D;'

                  '_m_h5_tk=3627342e3b09ae88a2c724b51c503d85_1559657695270;'
                  '_m_h5_tk_enc=c88d8e3f4ba68bc083f853e956656b6c;'
                  'x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0;'
                  'x5sec=7b22726174656d616e616765723b32223a22623034623263306164386231666234336634303330663463646430663064653743504c4f32656346454e364e6b736a586d664c7373774561444449334d4445304d4455794e4445374d513d3d227d;'
                  'whl=-1%260%260%261559652412550;'
                  'l=bB_eivGRvlAmYuqwBOfgiuI8a17TwIdfhsPzw4_G2ICP_bfDwBcPWZTPLZTkC3GVa6GXR3oyuHLBBW8iLyUCh;'
                  'isg=BGNjWHeLO7xJIPCeqwvJFNPV8qfN8Pf9ajjwFJXBBkM21IT2HSu26rnCyuT_9E-S',

        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',

        'Connection': 'keep-alive',
        'Host': 'rate.taobao.com',
    }
    html = requests.get(url=url, headers=headers)
    print(html.text)
    json_str = html.text
    #清洗步骤（获取的字符串前后有括号）--去掉str前后括号
    json_str = json_str.strip().lstrip('(').rstrip(')')

    #str to JSON（dict）
    JSONlist = initlist(json_str)
    print(JSONlist)
    # for i in JSONlist:
    #     print(i['好评图片'])
    #     try:
    #         with open('123.txt', 'a+', encoding='utf-8') as f:
    #             f.write(str(i) + '\n')
    #     except:
    #         print('cuole')
    #         pass
    return JSONlist


# if __name__ == '__main__':
#     itemid = 588976817331
#     page = 1
#     tbmain(itemid, page)